package com.fxd.shopping.product.service.impl;

import com.fxd.core.util.PageUtils;
import com.fxd.core.util.Query;
import com.fxd.shopping.product.dto.AttrGroupWithAttrDTO;
import com.fxd.model.product.AttrEntity;
import com.fxd.shopping.product.service.AttrService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.fxd.shopping.product.dao.AttrGroupDao;
import com.fxd.model.product.AttrGroupEntity;
import com.fxd.shopping.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, long catelogId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        if (StringUtils.isNotBlank(key)) {
            wrapper.and((obj) -> {
                obj.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }
        if (catelogId == 0) {
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        } else {
            wrapper.eq("catelog_id", catelogId);
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
            return new PageUtils(page);
        }
    }

    @Override
    public List<AttrGroupWithAttrDTO> getAttrGroupWithAttrsByCatelogId(Long catelogId) {

        //1，查询分组信息
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));

        //2，查询属性
        List<AttrGroupWithAttrDTO> collect = attrGroupEntities.stream().map((item) -> {
            AttrGroupWithAttrDTO attrGroupWithAttrDTO = new AttrGroupWithAttrDTO();
            BeanUtils.copyProperties(item, attrGroupWithAttrDTO);

            List<AttrEntity> relationAttr = attrService.getRelationAttr(attrGroupWithAttrDTO.getAttrGroupId());
            attrGroupWithAttrDTO.setAttrs(relationAttr);
            return attrGroupWithAttrDTO;
        }).collect(Collectors.toList());

        return collect;
    }
}